package framework.controller;

import com.google.common.collect.Lists;
import framework.bo.SysMenu;
import framework.common.Common;
import framework.common.ResponseVo;
import framework.service.SysMenuService;
import framework.service.SysUserRoleService;
import framework.util.jdkExt._CollectionUtil;
import framework.util.layui._LayuiUtil;
import framework.util.queryFilter.service.impl.QueryFilter;
import framework.util.queryFilter.vo.Where;
import frameworkExt.util.controller.BaseControllerExt;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 *
 * @author 吴宇亮 on 2019/2/7 0007 17:42
 */
@Controller
@RequestMapping("/framework/SysMenu.do")
public class SysMenuController extends BaseControllerExt {

    @Resource
    private SysMenuService service;
    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 如果是系统管理员等特殊角色则不需要进行菜单权限过滤
     */
    @ResponseBody
    @RequestMapping(params = "method=listTree")
    public ResponseVo listTree(){
        List<Integer> roles = sysUserRoleService.getSysRoleLidsByCurrentUser();
        if(CollectionUtils.isEmpty(roles)){
            return sendSuccess(new ArrayList<SysMenu>());
        }else if(roles.contains(Common.ROLE_ADMIN)){
            QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
            filter.where(SysMenu.PLID, Where.eq, "-1");
            filter.where(SysMenu.IS_DELETE, Where.eq, Common.IS_NO);
            filter.where(SysMenu.IS_USE, Where.eq, Common.IS_YEW);
            List<SysMenu> list = service.getAll(filter);
            list.forEach(menu -> getChild(menu, roles));
            return sendSuccess(list);
        }else{
            Map<String, String> requestMap = new HashMap<>();
            requestMap.put("where_sm.plid_eq", "-1");
            requestMap.put("where_sm.isDelete_eq", Common.IS_NO + "");
            requestMap.put("where_sm.isUse_eq", Common.IS_YEW + "");
            requestMap.put("where_srm.roleLid_in", _CollectionUtil.join(roles, ","));
            List<SysMenu> list = service.list(requestMap);
            list.forEach(menu -> getChild(menu, roles));
            return sendSuccess(list);
        }
    }

    /**
     * 获取该Menu下的子菜单, 如果有子菜单, 则set到childList里
     * @param fatherSysMenu
     */
    private void getChild(SysMenu fatherSysMenu, List<Integer> roles) {
        if(roles.contains(Common.ROLE_ADMIN)){
            QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
            filter.where(SysMenu.PLID, Where.eq, fatherSysMenu.getLid());
            filter.where(SysMenu.IS_DELETE, Where.eq, Common.IS_NO);
            filter.where(SysMenu.IS_USE, Where.eq, Common.IS_YEW);
            List<SysMenu> childList = service.getAll(filter);
            if(childList.size() > 0){
                fatherSysMenu.setChildList(childList);
                childList.forEach(menu -> {
                    getChild(menu, roles);
                });
            }
        }else{
            Map<String, String> requestMap = new HashMap<>();
            requestMap.put("where_sm.plid_eq", fatherSysMenu.getLid() + "");
            requestMap.put("where_sm.isDelete_eq", Common.IS_NO + "");
            requestMap.put("where_sm.isUse_eq", Common.IS_YEW + "");
            requestMap.put("where_srm.roleLid_in", _CollectionUtil.join(roles, ","));

            List<SysMenu> childList = service.list(requestMap);
            if(childList.size() > 0){
                fatherSysMenu.setChildList(childList);
                childList.forEach(menu -> {
                    getChild(menu, roles);
                });
            }
        }
    }

    @ResponseBody
    @RequestMapping(params = "method=list")
    public ResponseVo list(){
        QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
        filter.where("cname", Where.like, "测试");
        filter.where("lid", Where.eq, 1000);

        List<SysMenu> list = service.getAll(filter);
        return sendSuccess(list);
    }

    @ResponseBody
    @RequestMapping(params = "method=add")
    public ResponseVo add(){
        SysMenu sysMenu = new SysMenu();
        sysMenu.setCname("test123321");
        sysMenu.setHaschild(0);
        service.add(sysMenu);
        return sendSuccess();
    }

    @ResponseBody
    @RequestMapping(params = "method=addBatch")
    public ResponseVo addBatch(){
        int count = 100000;
        ArrayList list = new ArrayList(count);
        for(int i = 0; i < count; i++){
            SysMenu sysMenu = new SysMenu();
            sysMenu.setCname("测试"+i);
            list.add(sysMenu);
        }

        long start = System.currentTimeMillis();
        service.addBatch(list);
        long end = System.currentTimeMillis();

        return sendSuccess("插入"+count+"条数据, 耗时"+(end-start)+"毫秒");
    }

    @ResponseBody
    @RequestMapping(params = "method=update")
    public ResponseVo update(){
        SysMenu sysMenu = new SysMenu();
        sysMenu.setLid(134);
        sysMenu.setCname("test123321");
        sysMenu.setHaschild(0);
        service.update(sysMenu);

        return sendSuccess();
    }

    @ResponseBody
    @RequestMapping(params = "method=addMenu")
    public ResponseVo addMenu(){
        SysMenu m1 = new SysMenu();
        m1.setCname("菜单1");

        SysMenu m2 = new SysMenu();
        m2.setCname("菜单2");

        ArrayList<SysMenu> list = Lists.newArrayList(m1, m2);
        service.addBatch(list);

        list.forEach(item -> {
            System.out.println(item.getLid() + ": " + item.getCname());
        });
        return sendSuccess(list);
    }

    @ResponseBody
    @RequestMapping(params = "method=removeBatch")
    public ResponseVo removeBatch(){
        service.removeById(122);

        int count = 100;
        ArrayList list = new ArrayList(count);
        for(int i = 10000; i < 10000+count; i++){
            SysMenu sysMenu = new SysMenu();
            sysMenu.setLid(i);
            list.add(sysMenu);
        }

        long start = System.currentTimeMillis();
        service.removeBatch(list);
        long end = System.currentTimeMillis();

        return sendSuccess("删除"+count+"条数据, 耗时"+(end-start)+"毫秒");
    }
}
